import re

from qturboframe.lib.webdriver.exceptions import SeleniumError
from qturboframe.lib.utils.check import CheckBasic
from qturboframe.public_ui.base_element import BaseElement


class Transfer(BaseElement, CheckBasic):

    def __init__(self, element_para, father=None, timeout=5):
        super().__init__(element_para, father, timeout)
        self.left_element = self._element.find_element_by_attribute(
            'css selector', 'div.h-transfer-list:first-child', timeout=self.timeout)
        self.right_element = self._element.find_element_by_attribute(
            'css selector', 'div.h-transfer-list:last-child', timeout=self.timeout)
        self.operate_element = self._element.find_element_by_attribute(
            'css selector', 'div.h-transfer-operation', timeout=self.timeout)

    def set_value_for_multiple(self, value=['all']):
        '''
        hui的穿梭框选择

        :Args:
         - value: 选择项的文本集合

        :Example:
         - 例: transfer.set_value_for_multiple(['苹果','葡萄'])
        '''
        re_checked = 'h-checkbox-checked'
        if len(value) == 1 and value[0] == 'all':
            all_right_btn = self.operate_element.find_elements_by_attribute(
                'xpath', './button[contains(@class, "h-btn-ghost")]', timeout=self.timeout)[1]
            all_right_btn.secure_click()
        else:
            for text in value:
                select_li = self.left_element.find_element_by_attribute(
                    'css selector', 'ul.h-transfer-list-content li.h-transfer-list-content-item',
                    'text:' + text, timeout=self.timeout)
                span = select_li.find_element_by_attribute(
                    'css selector', 'span.h-checkbox', timeout=self.timeout)
                if not re.search(re_checked, span.get_attribute('class')):
                    span.secure_click()
            right_btn = self.operate_element.find_elements_by_attribute(
                'xpath', './button[contains(@class, "h-btn-primary")]', timeout=self.timeout)[1]
            right_btn.secure_click()

    def is_default(self, value):
        '''
        hui的穿梭框默认值校验

        :Args:
         - value: 校验的默认值集合

        :Returns:
            匹配返回True，否则返回False

        :Example:
         - 例: transfer.is_default(['苹果','葡萄'])
        '''
        li_list = self.right_element.find_elements_by_attribute(
            'css selector', 'ul.h-transfer-list-content li.h-transfer-list-content-item',
            timeout=self.timeout)
        if len(value) == len(li_list):
            for li in li_list:
                if li.text.strip() not in value:
                    return False
            else:
                return True
        else:
            return False

    def clear(self):
        '''
        清除已选项

        :Example:
         - 例: transfer.clear()
        '''
        all_left_btn = self.operate_element.find_elements_by_attribute(
            'xpath', './button[contains(@class, "h-btn-ghost")]', timeout=self.timeout)[0]
        all_left_btn.secure_click()

    def check_default(self, value):
        '''
        hui的穿梭框默认值校验

        :Args:
         - value: 校验的默认值集合

        :Raises:
            SeleniumError: 穿梭框的默认值校验不通过

        :Example:
         - 例: transfer.check_default(['苹果','葡萄'])
        '''
        if not self.is_default(value):
            raise SeleniumError('穿梭框的默认值校验不通过！')

